package evi.com.evi.fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import evi.com.evi.R;
import evi.com.evi.activity.SearchActivity;
import evi.com.evi.adapter.OrderDisposeAdapter;
import evi.com.evi.pager.BasePager;
import evi.com.evi.pager.PendingOrderPager;
import evi.com.evi.pager.ToPickUpOrderPager;

import static android.content.ContentValues.TAG;

/**
 * 未处理
 */
public class DisposeFalseFragment extends Fragment implements View.OnClickListener {
    private View view;

//    private ImageView iv_search;

    //xuy add begin
    private ImageView iv_cancel;//清除
    public EditText searchText;//搜索内容et_search
    //xuy add end

    private RadioButton tv_wait_takeorder;
    private RadioButton tv_wait_getgoods;
    private View view_line_right;
    private View view_line_left;

    private ViewPager vp_dispose;
    private RadioGroup rg_dispose;

    private List<BasePager> basePagers;

    //    private XListView lv_order;
    private OrderDisposeAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_dispose_false, container, false);
        initView();
        return view;
    }

    private void initView() {
//        iv_search = (ImageView) view.findViewById(R.id.iv_search);
//        iv_search.setOnClickListener(this);
        tv_wait_takeorder = (RadioButton) view.findViewById(R.id.tv_wait_takeorder);
        tv_wait_getgoods = (RadioButton) view.findViewById(R.id.tv_wait_getgoods);

        //xuy begin
        //搜索内容及清除图标
        iv_cancel = (ImageView) view.findViewById(R.id.iv_search_icon);
        searchText = (EditText) view.findViewById(R.id.et_search);
        iv_cancel.setOnClickListener(this);
        //xuy end

        view_line_right = (View) view.findViewById(R.id.view_line_right);
        view_line_left = (View) view.findViewById(R.id.view_line_left);

        vp_dispose = (ViewPager) view.findViewById(R.id.vp_dispose);
        rg_dispose = (RadioGroup) view.findViewById(R.id.rg_dispose);

        basePagers = new ArrayList<>();
        basePagers.add(new PendingOrderPager(getActivity(), this));
        basePagers.add(new ToPickUpOrderPager(getActivity(), this));

        vp_dispose.setAdapter(new MyPagerAdapter());

        rg_dispose.setOnCheckedChangeListener(new MyOnCheckdeChangeListener());

        vp_dispose.setCurrentItem(0);

        rg_dispose.check(R.id.tv_wait_takeorder);

        vp_dispose.setOnPageChangeListener(new MyOnPagerChangeListener());

        basePagers.get(0).initData();
        basePagers.get(1).initData();

        tv_wait_takeorder.setOnClickListener(this);
        tv_wait_getgoods.setOnClickListener(this);

        Log.e(TAG, "待确认订单加载+++++++++++++");



        searchText.addTextChangedListener( new TextWatcher(){

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Log.e("eee","onTextChanged."+charSequence.toString()+",i="+i+",i1="+i1+",i2="+i2);
                basePagers.get(0).flushFilter(""+charSequence.toString());
                basePagers.get(1).flushFilter(""+charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    /**
     *
     */
    public void getCount(BasePager pager, String count) {
        if (count == null) {
            count = "0";
        }

        if (pager instanceof PendingOrderPager) {
            tv_wait_takeorder.setText("待确认 [" + count + "]");
        } else if (pager instanceof ToPickUpOrderPager) {
            tv_wait_getgoods.setText("待取货 [" + count + "]");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //xuy add begin
            case R.id.iv_search_icon:
                //清除搜索内容
                searchText.setText("");
//                basePagers.get(0).flushFilter("");
//                basePagers.get(1).flushFilter("");

                break;
            //xuy add end
            case R.id.iv_search:        //搜索
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                intent.putExtra("ActType", 1);
                startActivity(intent);
                break;
            case R.id.tv_wait_takeorder:        //待接单
                setItemselect(tv_wait_takeorder);
                setView(R.color.pub_textcolor_yellow, View.VISIBLE, R.color.pub_textcolor_graylevel01, View.INVISIBLE);
                break;

            case R.id.tv_wait_getgoods:        //待取货
                setItemselect(tv_wait_getgoods);
                setView(R.color.pub_textcolor_graylevel01, View.INVISIBLE, R.color.pub_textcolor_yellow, View.VISIBLE);
                break;
            default:
                break;
        }
    }


    private void setItemselect(TextView tv) {
        tv_wait_takeorder.setBackgroundColor(getResources().getColor(R.color.pub_color_white));
        tv_wait_getgoods.setBackgroundColor(getResources().getColor(R.color.pub_color_white));
        tv.setBackgroundColor(getResources().getColor(R.color.pub_color_white));
    }

    public void setView(int tv_left, int line_left, int tv_right, int line_right) {
        tv_wait_takeorder.setTextColor(getResources().getColor(tv_left));
        view_line_left.setVisibility(line_left);
        tv_wait_getgoods.setTextColor(getResources().getColor(tv_right));
        view_line_right.setVisibility(line_right);
    }


    private class MyPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return basePagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePager basePager = basePagers.get(position);
            View rootView = basePager.rootView;
            container.addView(rootView);
            return rootView;

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    private class MyOnCheckdeChangeListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.tv_wait_takeorder:
                    vp_dispose.setCurrentItem(0);
                    setItemselect(tv_wait_takeorder);
                    setView(R.color.pub_textcolor_yellow, View.VISIBLE, R.color.pub_textcolor_graylevel01, View.INVISIBLE);
                    break;
                case R.id.tv_wait_getgoods:
                    vp_dispose.setCurrentItem(1);
                    setItemselect(tv_wait_getgoods);
                    setView(R.color.pub_textcolor_graylevel01, View.INVISIBLE, R.color.pub_textcolor_yellow, View.VISIBLE);
                    break;
            }
        }
    }

    private class MyOnPagerChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            basePagers.get(position).initData();
            ((RadioButton) rg_dispose.getChildAt(position)).setChecked(true);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }


    public void restart(int tag){
        Log.e(TAG, "restart: " + tag );
        if (basePagers != null) {
            initPager();
            if(tag == 2){
                vp_dispose.setCurrentItem(0);
            }
        }
    }

    public void initPager() {
        basePagers.get(0).initData();
        basePagers.get(1).initData();
    }


}
